
package com.sunny.hp.common.util; 

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.sunny.hp.common.servlet.ModelAndNCOMZView;

/**
 * <pre>
 * com.ncomz.nzeip.common.util 
 *    |_ HTMLParseUtil.java
 * 
 * </pre>
 * @date : 2014. 6. 10. 오후 4:09:02
 * @version : 
 * @author : P044826
 */
@Controller
@RequestMapping("/tax")
public class HTMLParseUtil {

	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * <pre>
	 * 1. 개요 : 
	 * 2. 처리내용 : 
	 * </pre>
	 * @Method Name : selectTaxCodeDataList
	 * @date : 2014. 6. 10.
	 * @author : P044826
	 * @history : 
	 *	-----------------------------------------------------------------------
	 *	변경일				작성자						변경내용  
	 *	----------- ------------------- ---------------------------------------
	 *	2014. 6. 10.		P044826				최초 작성 
	 *	-----------------------------------------------------------------------
	 * 
	 * @param code
	 * @return
	 */ 
	@RequestMapping("/codeinfo.do")
	public ModelAndNCOMZView selectTaxCodeDataList(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndNCOMZView mav = new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		UtilHelper csUtil = new UtilHelper(); 
		LinkedHashMap map = requestUtil.paramBind();
		
		LinkedHashMap<String, String> returnMap = new LinkedHashMap<String, String>();
		URL url = null;
		URLConnection urlConnection = null;

		// URL 주소
		String sUrl = "http://www.nts.go.kr/cal/cal_04_01.asp";
		String now_year = csUtil.getString(map.get("now_year"), "");
		String gijun_code = csUtil.getString(map.get("gijun_code"), "");
		try {
			// Get방식으로 전송 하기
			url = new URL(sUrl + "?" + "now_year=" + now_year + "&gijun_code=" + gijun_code);
			urlConnection = url.openConnection();
			returnMap = printByInputStream(urlConnection.getInputStream());
		} catch(Exception e) {
			e.printStackTrace();
		}
		mav.addObject("codeinfo", returnMap);
		return mav;
	}
	
	// 웹 서버로 부터 받은 웹 페이지 결과를 콘솔에 출력하는 메소드
	public LinkedHashMap<String, String> printByInputStream(InputStream is) {
		
		LinkedHashMap<String, String> returnMap = new LinkedHashMap<String, String>();
		byte[] buf = new byte[1024];
		int len = -1;
		StringBuffer bufs = new StringBuffer();
		try {
			while((len = is.read(buf, 0, buf.length)) != -1) {
				//System.out.write(buf, 0, len);
				bufs.append(  new String( buf )  );
			}
			
			org.jsoup.nodes.Document doc = Jsoup.parse(bufs.toString());
			
			Elements elements = doc.select("#table_view");
			Elements elements_list = elements.select("tr");			
			
			List<Element> ele = elements_list.subList(0, 7);
			int k = 0;
			for(Element el : ele){
				returnMap.put(el.select("td").get(0).text(), el.select("td").get(1).text());
			}

		} catch(IOException e) {
			e.printStackTrace();
		}
		
		return returnMap;
	}

}
